package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.sam.im.heiqun.R;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.EventBusType;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectionOfPresentTypeActivity extends BaseSwipeBackActivity implements CompoundButton.OnCheckedChangeListener {

    private static final String TAG = SelectionOfPresentTypeActivity.class.getSimpleName();
    @BindView(R.id.linear_mainland)
    LinearLayout linearMainland;
    @BindView(R.id.linear_overseas)
    LinearLayout linearOverseas;

    @BindView(R.id.mainland)
    CheckBox mainland;
    @BindView(R.id.overseas)
    CheckBox overseas;

    @BindView(R.id.next)
    Button next;

    private int type = 0;      // 0 大陆     1 overseas

    @Override
    protected String getTitleText() {
        return getString(R.string.select_putforward_type);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mainland.setOnCheckedChangeListener(this);
        overseas.setOnCheckedChangeListener(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onBaseEvent(final Integer refresh) {
        if (refresh == EventBusType.RESULT_PAY_WX_SUCCESS){
            finish();
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_select_ofpresent_type;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.mainland:
                if (!isChecked && type == 0) {
                    mainland.setChecked(true);
                    overseas.setChecked(false);
                } else if (isChecked && type == 1) {
                    type = 0;
                    mainland.setChecked(true);
                    overseas.setChecked(false);
                }
                break;
            case R.id.overseas:
                if (!isChecked && type == 1) {
                    overseas.setChecked(true);
                    mainland.setChecked(false);
                } else if (isChecked && type == 0) {
                    type = 1;
                    overseas.setChecked(true);
                    mainland.setChecked(false);
                }
                break;
        }
    }

    @OnClick({R.id.next,R.id.linear_overseas,R.id.linear_mainland})
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.next:
                doNext();
                break;
            case R.id.linear_mainland:
                mainland.setChecked(true);
                break;
            case R.id.linear_overseas:
                overseas.setChecked(true);
                break;
        }
    }

    private void doNext() {
        Intent intent = new Intent(this, PutForwardActivity.class);
        intent.putExtra("type",type+"");
        startActivity(intent);
    }
}
